package pixelwaks.ctrl.impl;

import java.awt.Point;

import common.core.Tile;
import common.ctrl.IObserver;
import common.gui.IViewable;
import common.util.Calculate;

import pixelwaks.core.impl.Game;

public class SceneryCtrl implements IObserver {

	private IViewable observer;

	public void checkIfGotHit(Point p, int strength) {
		
		int startXpos = (p.x - (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int startYpos = (p.y - (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int slutXpos  = (p.x + (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;
		int slutYpos  = (p.y + (strength * Tile.TILE_SIZE)) / Tile.TILE_SIZE;	
		int radius = strength * Tile.TILE_SIZE + (Tile.TILE_SIZE / 2);
		Tile[][] tiles = Game.getInstance().getScenery().getTiles();
		
		for(int i = startYpos; i <= slutYpos; i++) {
			for(int j = startXpos; j <= slutXpos; j++) {
				if(i >= 0 && i < tiles.length && j >= 0 && j < tiles[i].length) {												
						if(tiles[i][j] != null && tiles[i][j].isDestructible() && Calculate.isInBlastArea(p.x, p.y, j*Tile.TILE_SIZE+5, i*Tile.TILE_SIZE+5, radius)) {
							tiles[i][j] = null;
						}
				}
			}
		}		
		observer.updateView(null);
	}

	@Override
	public void addView(IViewable iv) {
		
		observer = iv;
	}
}
